package com.anlu.base.jdk.jdk8.functions;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * <p>
 * Description:Predicate<T>
 *
 * 抽象方法：boolean test(T t)
 * 用途：表示一个断言，接受一个输入参数并返回一个布尔值。
 * 使用场景：当需要对输入参数进行条件判断时，可以使用Predicate。
 * <p>
 * <p>
 *
 * @author anlu
 * @date 2024/9/24 14:12
 */
public class FilterFruits {
    public static void main(String[] args) {
        List<Fruit> fruits = new ArrayList<>();
        fruits.add(new Fruit("Apple", "Red", 100));
        fruits.add(new Fruit("Banana", "Yellow", 150));
        fruits.add(new Fruit("Orange", "Orange", 120));

        List<Fruit> redFruits = filterFruits(fruits, fruit -> fruit.getColor().equals("Red"));
        System.out.println("Red fruits: " + redFruits);
    }

    public static List<Fruit> filterFruits(List<Fruit> fruits, Predicate<Fruit> predicate) {
        List<Fruit> filteredFruits = new ArrayList<>();
        for (Fruit fruit : fruits) {
            if (predicate.test(fruit)) {
                filteredFruits.add(fruit);
            }
        }
        return filteredFruits;
    }
}

class Fruit {
    private String name;
    private String color;
    private int weight;

    public Fruit(String name, String color, int weight) {
        this.name = name;
        this.color = color;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

    public String getColor() {
        return color;
    }

    public int getWeight() {
        return weight;
    }

    @Override
    public String toString() {
        return "Fruit{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                ", weight=" + weight +
                '}';
    }
}
